# > python kahelo2zip.py

# build the excutable dist folder
# add required dlls and help file
# compress and clean
# name zipfile as kahelo-exe-<version>.zip, version found in kahelo.py
# make kahelo-py-<version>.zip

import os
import re
import shutil
import zipfile

# find version number
with open('kahelo.py') as f:
    for line in f:
        m = re.search("VERSION += +'([^ ]+)'", line)
        if m:
            version = m.group(1)
            break

# build exe dist folder
cmd = os.popen('kahelo2exe.py py2exe')
cmd.read()

# copy msvc dll to dist folder
for file in os.listdir('msvcdll'):
    shutil.copyfile(os.path.join('msvcdll', file), os.path.join('dist', file))

# copy help file to dist folder
shutil.copyfile('kahelo.html', os.path.join('dist', 'kahelo.html'))

# rename
os.rename('dist', 'kahelo')

# compress
with zipfile.ZipFile('kahelo-exe-%s.zip' % version, 'w', zipfile.ZIP_DEFLATED) as myzip:
    for file in os.listdir('kahelo'):
        myzip.write(os.path.join('kahelo', file))

shutil.rmtree('kahelo')
shutil.rmtree('build')

# make script package
with zipfile.ZipFile('kahelo-py-%s.zip' % version, 'w', zipfile.ZIP_DEFLATED) as myzip:
    myzip.write('kahelo.py')
    myzip.write('kahelo.html')
